//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    //二叉树
    public static void main(String[] args) {
        //1.BM1二叉树的前序遍历
        public int[] preorderTraversal (TreeNode root) {
            List<Integer> list = new ArrayList<>();
            preorder(list, root);
            int[] array = new int[list.size()];
            for (int i = 0; i < list.size(); i++) {
                array[i] = list.get(i);
            }
            return array;
        }
        public void preorder(List<Integer> list, TreeNode root) {
            if (root == null) {
                return;
            }
            list.add(root.val);
            preorder(list, root.left);
            preorder(list, root.right);
        }
        //2.BM2二叉树的中序遍历
        public int[] inorderTraversal (TreeNode root) {
            List<Integer> list = new ArrayList<>();
            inorder(list, root);
            int[] array = new int[list.size()];
            for (int i = 0; i < list.size(); i++) {
                array[i] = list.get(i);
            }
            return array;
        }
        public void inorder(List<Integer> list, TreeNode root) {
            if (root == null) {
                return;
            }
            inorder(list, root.left);
            list.add(root.val);
            inorder(list, root.right);
        }
        //3.BM3二叉树的后序遍历
        public int[] postorderTraversal (TreeNode root) {
            List<Integer> list = new ArrayList<>();
            postorder(list, root);
            int[] array = new int[list.size()];
            for (int i = 0; i < list.size(); i++) {
                array[i] = list.get(i);
            }
            return array;
        }
        private void postorder(List<Integer> list,TreeNode root) {
            if(root == null) {
                return;
            }
            postorder(list,root.left);
            postorder(list,root.right);
            list.add(root.val);
        }
    }
}